package com.augurit.smartform.web.parser;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.smartform.database.DatabaseExecutor;
import com.augurit.smartform.database.SourceData;
import com.augurit.smartform.display.PageExecutor;
import com.augurit.smartform.exception.FormException;
import com.augurit.smartform.exception.FormMessage;
import com.augurit.smartform.service.IWfSupportService;
import com.augurit.smartform.test.TestUtils;


@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.SUCCESS, location = "/smartform/success.jsp")
})
public class ParseAction extends ExtBaseAction {
	@Autowired
	private IWfSupportService wfSupportService;
	@Autowired
	private DatabaseExecutor databaseExecutor;
	@Autowired
	private PageExecutor pageExecutor;
	
	/*
	@Override
	public String execute() throws Exception {
		List<FormMessage> messages=new ArrayList<FormMessage>();
		FormMessage message=new FormMessage();
		SourceData source = null;
		try{
			source = TestUtils.newFindInst1();
			TestUtils.printData(source);
			
			message=this.executeSinglePage(source);
			messages.add(message);
			
			source = TestUtils.newFindInst2();
			TestUtils.printData(source);
			
			message=this.executeSinglePage(source);
			messages.add(message);
			
		}catch(Exception e){
			message.setFailure(e.getMessage());
			messages.add(message);
		}
		String jsonStr=Struts2Utils.getJsonString(messages);
		this.getRequest().setAttribute("formPath", jsonStr);
		return SUCCESS;
	}
	
	public void createAny(){
		List<FormMessage> messages=new ArrayList<FormMessage>();
		FormMessage message=new FormMessage();
		SourceData source = null;
		try{
			int pageCount=ftl.length;
			if(pageCount!=xml.length
				||pageCount!=html.length
				||pageCount!=clientJs.length
				||pageCount!=serverJs.length){
				throw new FormException("表单设计器传入参数有缺失!");
			}
			for(int i=0;i<pageCount;i++){
				source=new SourceData();
				source.setXml(xml[i]);
				source.setFtl(ftl[i]);
				source.setHtml(html[i]);
				source.setClientJs(clientJs[i]);
				source.setServerJs(serverJs[i]);
				message=this.executeSinglePage(source);
				messages.add(message);
			}
		}catch(Exception e){
			message.setFailure(e.getMessage());
			messages.add(message);
		}
		this.renderJson(messages);
	}
	
	private FormMessage executeSinglePage(SourceData source){
		FormMessage message=new FormMessage();
		try{
			source.setAbsolutePath(getAbsolutePath());
	
			//创建数据库
			databaseExecutor.execute(this.getLoginUserName(), source.getXml());
	
			//解析页面
			String path = pageExecutor.execute(source);
			
			//进行工作流相关操作
			wfSupportService.saveElementAndRef(source);
			
			String formPath = getRootPath() + getRequest().getContextPath() + path;
			message.setSuccess(formPath);
			
			return message;
		}catch(Exception e){
			message.setFailure(e.getMessage());
			return message;
		}
	}
	*/
	
	@Override
	public String execute() throws Exception {
		FormMessage message=new FormMessage();
		try{
			source = TestUtils.newFindInst();
			TestUtils.printData(source);
			
			source.setAbsolutePath(getAbsolutePath());
	
			//创建数据库
			databaseExecutor.execute(this.getLoginUserName(), source.getXml());
	
			//解析页面
			String path = pageExecutor.execute(this.getLoginUserName(),source);
			
			//进行工作流相关操作
			wfSupportService.saveElementAndRef(source);
			
			String formPath = getRootPath() + getRequest().getContextPath() + path;
			message.setSuccess(formPath);
		}catch(Exception e){
			message.setFailure(e.getMessage());
		}
		String jsonStr=Struts2Utils.getJsonString(message);
		this.getRequest().setAttribute("formPath", jsonStr);
		return SUCCESS;
	}
	
	public void createAny(){
		FormMessage message=new FormMessage();
		try{
			source.setAbsolutePath(getAbsolutePath());
	
			//创建数据库
			databaseExecutor.execute(this.getLoginUserName(), source.getXml());
	
			//解析页面
			String path = pageExecutor.execute(this.getLoginUserName(),source);
			
			//进行工作流相关操作
			wfSupportService.saveElementAndRef(source);
			
			String formPath = getRootPath() + getRequest().getContextPath() + path;
			message.setSuccess(formPath);
		}catch(FormException e){
			message.setFailure(e.getMessage());
		}
		this.renderJson(message);
	}
	
	private SourceData source=new SourceData();
	public SourceData getSource() {
		return source;
	}
	public void setSource(SourceData source) {
		this.source = source;
	}
	
	/*
	private String[] xml;
	private String[] html;
	private String[] ftl;
	private String[] clientJs;
	private String[] serverJs;
	
	public String[] getFtl() {
		return ftl;
	}

	public String[] getXml() {
		return xml;
	}

	public void setXml(String[] xml) {
		this.xml = xml;
	}

	public String[] getHtml() {
		return html;
	}

	public void setHtml(String[] html) {
		this.html = html;
	}

	public String[] getClientJs() {
		return clientJs;
	}

	public void setClientJs(String[] clientJs) {
		this.clientJs = clientJs;
	}

	public String[] getServerJs() {
		return serverJs;
	}

	public void setServerJs(String[] serverJs) {
		this.serverJs = serverJs;
	}

	public void setFtl(String[] ftl) {
		this.ftl = ftl;
	}
	*/
}
